package com.xt.mvcpilot.data;

import java.util.Collection;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * represent the role class in the user class
 * 
 * 
 * @author txiao
 *
 */
@Entity
@Table(name="ROLE")
@org.hibernate.annotations.Entity(
		dynamicInsert=true, //not insert null property 
		dynamicUpdate=true  //not update unchanged property
)
public class Role extends AppBasicDBObject{

	private static final long serialVersionUID = -6940566416201949150L;
	
	@NotEmpty(message="{role.validator.name.required}")
	@Size(min = 2, max = 40, message="{role.validator.name.required}")
	@Column(name = "name")
	private String name;
	
	@ManyToMany(
		targetEntity=Permission.class,
        cascade={CascadeType.PERSIST}
    )
    @JoinTable(
        name="ROLE_PERM",
        joinColumns=@JoinColumn(name="ROLEID"),
        inverseJoinColumns=@JoinColumn(name="PERMID")
    )
	private Set<Permission> perms;
	
	
//	@ManyToMany(
//		targetEntity=User.class,
//		cascade={CascadeType.PERSIST}
//    )
//    @JoinTable(
//        name="USER_ROLE",
//        joinColumns=@JoinColumn(name="ROLEID"),
//        inverseJoinColumns=@JoinColumn(name="USERID")
//    )
//    private Set<User> users;
    
	public Role(){}

	
	//Getters and Setters
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	//add a permission
	public void addPerm(Permission perm){
		perms.add(perm);
	}
	
	//delete a permission
	public void deletePerm(Permission perm){
		perms.remove(perm);
	}
	
	public Set<Permission> getPerms() {
		return perms;
	}


	public void setPerms(Set<Permission> perms) {
		this.perms = perms;
	}


//	public Set<User> getUsers() {
//		return users;
//	}
//
//
//	public void setUsers(Set<User> users) {
//		this.users = users;
//	}	
	
	public String getIdAsString() {
		return new Long(id).toString();
	}
	
}
